package com.koron.order.patrol.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.patrol.bean.po.PatrolEquipmentItemPo;
import com.koron.order.patrol.mapper.PatrolEquipmentItemMapper;
import com.koron.order.patrol.service.PatrolEquipmentItemService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 巡视工单-巡视点设备下检查项从表(PatrolEquipmentItem)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-26 15:26:25
 */
@Service("patrolEquipmentItemService")
public class PatrolEquipmentItemServiceImpl implements PatrolEquipmentItemService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, PatrolEquipmentItemPo patrolEquipmentItem) {

        PatrolEquipmentItemMapper mapper = factory.getMapper(PatrolEquipmentItemMapper.class);

        return mapper.insertOrUpdate(patrolEquipmentItem);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<PatrolEquipmentItemPo> patrolEquipmentItemList) {

        Assert.notEmpty(patrolEquipmentItemList, "检查项不能为空");

        List<String> patrolEquipmentIds = patrolEquipmentItemList.stream().map(PatrolEquipmentItemPo::getPatrolEquipmentId).distinct().collect(Collectors.toList());

        Assert.notEmpty(patrolEquipmentIds, "设备id不能为空");

        PatrolEquipmentItemMapper mapper = factory.getMapper(PatrolEquipmentItemMapper.class);

        //先根据 巡视工单ids删除
        mapper.deleteByPatrolEquipmentIds(patrolEquipmentIds);

        return mapper.insertBatch(patrolEquipmentItemList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, PatrolEquipmentItemPo patrolEquipmentItem) {

        PatrolEquipmentItemMapper mapper = factory.getMapper(PatrolEquipmentItemMapper.class);

        return mapper.insert(patrolEquipmentItem);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<PatrolEquipmentItemPo> patrolEquipmentItemList) {

        PatrolEquipmentItemMapper mapper = factory.getMapper(PatrolEquipmentItemMapper.class);

        return mapper.insertBatch(patrolEquipmentItemList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, PatrolEquipmentItemPo patrolEquipmentItem) {
        PatrolEquipmentItemMapper mapper = factory.getMapper(PatrolEquipmentItemMapper.class);
        return mapper.update(patrolEquipmentItem);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        PatrolEquipmentItemMapper mapper = factory.getMapper(PatrolEquipmentItemMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public PatrolEquipmentItemPo queryById(SessionFactory factory, String id) {

        PatrolEquipmentItemMapper mapper = factory.getMapper(PatrolEquipmentItemMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<PatrolEquipmentItemPo> queryList(SessionFactory factory, PatrolEquipmentItemPo patrolEquipmentItem) {

        PatrolEquipmentItemMapper mapper = factory.getMapper(PatrolEquipmentItemMapper.class);

        List<PatrolEquipmentItemPo> list = mapper.queryList(patrolEquipmentItem);

        return list;
    }

    @Override
    @TaskAnnotation("queryByPatrolEquipmentId")
    public List<PatrolEquipmentItemPo> queryByPatrolEquipmentId(SessionFactory factory, String patrolEquipmentId) {
        PatrolEquipmentItemPo patrolEquipmentItemPo = new PatrolEquipmentItemPo();
        patrolEquipmentItemPo.setPatrolEquipmentId(patrolEquipmentId);
        return queryList(factory, patrolEquipmentItemPo);
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, PatrolEquipmentItemPo patrolEquipmentItem, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        PatrolEquipmentItemMapper mapper = factory.getMapper(PatrolEquipmentItemMapper.class);

        List<PatrolEquipmentItemPo> list = mapper.queryList(patrolEquipmentItem);

        return new PageInfo(list);
    }

}